function HelipadMemo(xml)
{
  this.assignRawXml(xml);
}

HelipadMemo.prototype.assignRawXml=function(xml)
{
  var parser = new DOMParser();
  var tmp;
  var tagDocs;
  var counter;
  var serializer=new XMLSerializer();
  
  if(xml!=null)
  {
    this.rawXml=xml;
    this.xmlDoc=parser.parseFromString(this.rawXml,"text/xml");

    tmp=this.xmlDoc.getElementsByTagName("approved")[0].childNodes[0].nodeValue;
    this.approved=false;
    if(tmp!=null)
    {
      if(tmp!='')
      {
        if(tmp=='true')
          this.approved=true;
      }
    }

    this.createdOn=this.xmlDoc.getElementsByTagName("created-on")[0].childNodes[0].nodeValue;

    tmp=this.xmlDoc.getElementsByTagName("dangerous")[0].childNodes[0].nodeValue;
    this.dangerous=false;
    if(tmp!=null)
    {
      if(tmp!='')
      {
        if(tmp=='true')
          this.dangerous=true;
      }
    }

    //tmp=this.xmlDoc.getElementsByTagName("share")[0].childNodes[0].nodeValue;
    this.share=false;
    //if(tmp!=null)
    //{
    //  if(tmp!='')
    //  {
    //    if(tmp=='true')
    //      this.share=true;
    //  }
    //}

    if(this.xmlDoc.getElementsByTagName("source")[0].childNodes[0]!=null)
      this.source=this.xmlDoc.getElementsByTagName("source")[0].childNodes[0].nodeValue;
    else
      this.source='';
    this.title=this.xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
    this.updatedOn=this.xmlDoc.getElementsByTagName("updated-on")[0].childNodes[0].nodeValue;
    this.id=this.xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
    this.thunderNoteID=-1;
    this.tags=new Array();
    tagDocs=this.xmlDoc.getElementsByTagName('tag');
    for(counter=0;counter<tagDocs.length;counter++)
    {
      xml=serializer.serializeToString(tagDocs[counter]);
      this.tags[counter]=new HelipadTag(xml);
      if(this.tags[counter].name.indexOf('__TNAN__:')==0)
      {
        tmp=this.tags[counter].name.substring(9);
        this.thunderNoteID=parseInt(tmp,10);
      }
    }

    this.createTime=createDateFromHelipadDateString(this.createdOn);
    this.lastUpdateTime=createDateFromHelipadDateString(this.updatedOn);

  }
  else
  {
    this.rawXml=null;
    this.approved=false;
    this.createdOn=null;
    this.dangerous=false;
    this.share=false;
    this.source=null;
    this.title=null;
    this.updatedOn=null;
    this.id=-1;
    this.thunderNoteID=-1;
    this.tags=new Array();
    this.createTime=null;
    this.lastUpdateTime=null;
  }
}

function HelipadTitle(xml)
{
  this.assignRawXml(xml);
}

HelipadTitle.prototype.assignRawXml=function(xml)
{
  var parser = new DOMParser();
  
  if(xml!=null)
  {
    this.rawXml=xml;
    this.xmlDoc=parser.parseFromString(this.rawXml,"text/xml");
    this.title=this.xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
    this.id=this.xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
  }
  else
  {
    this.rawXml=null;
    this.xmlDoc=null;
    this.title=null;
    this.id=-1;
  }
}

function HelipadTag(xml)
{
  this.assignRawXml(xml);
}

HelipadTag.prototype.assignRawXml=function(xml)
{
  var parser = new DOMParser();
  
  if(xml!=null)
  {
    this.rawXml=xml;
    this.xmlDoc=parser.parseFromString(this.rawXml,"text/xml");
    this.name=this.xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
  }
  else
  {
    this.rawXml=null;
    this.xmlDoc=null;
    this.name=null;
  }
}

function createDateFromHelipadDateString(theDate)
{
  //           1         2
  // 012345678901234567890123456
  // 2009-08-31T01:05:11+01:00
  // 20:05 EST

  var RetVal=new Date();

  var yearString;
  var monthString;
  var dayString;
  var hourString;
  var minuteString;
  var secondString;
  var difHourString;
  var difMinuteString;
  var difPlusOrMinus;
  var tmpVal;
  var additionalHourOffset=0;
  var additionalDayOffset=0;
  var additionalMonthOffset=0;
  var additionalYearOffset=0;
  
  var yearInt,monthInt,dayInt,hourInt,minuteInt,secondInt;
  var monthdays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var isLeap=false;
  
  yearString=theDate.substring(0,4);
  monthString=theDate.substring(5,7);
  dayString=theDate.substring(8,10);
  hourString=theDate.substring(11,13);
  minuteString=theDate.substring(14,16);
  secondString=theDate.substring(17,19);
  difPlusOrMinus=theDate.substring(19,20);
  difHourString=theDate.substring(20,22);
  difMinuteString=theDate.substring(23);
  
  // get seconds
  secondInt=parseInt(secondString,10);

  // get minutes adjusted for the difference to UTC
  if(difPlusOrMinus=='+')
    minuteInt=parseInt(minuteString,10)-parseInt(difMinuteString,10);
  if(difPlusOrMinus=='-')
    minuteInt=parseInt(minuteString,10)+parseInt(difMinuteString,10);
  if(minuteInt<0)
  {
    // the difference went backward, adjust minutes into the pervious hour and
    // add an hour offset
    minuteInt+=60;
    additionalHourOffset--;
  }
  if(minuteInt>59)
  {
    minutInt-=60;
    additionalHourOffset++;
  }

  // get hours adjusted for the difference to UTC and any offset caused by minutes rolling
  // back or forward into a different hour 
  if(difPlusOrMinus=='+')
    hourInt=parseInt(hourString,10)-parseInt(difHourString,10);
  if(difPlusOrMinus=='-')
    hourInt=parseInt(hourString,10)+parseInt(difHourString,10);
  hourInt+=additionalHourOffset;
  if(hourInt<0)
  {
    // the difference went backward, adjust hours into the pervious day and
    // add a day offset
    hourInt+=24;
    additionalDayOffset--;
  }
  if(hourInt>23)
  {
    hourInt-=24;
    additionalDayOffset++;
  }
  
  // get days and adjust for any offset casued by the hours rolling back or forward into
  // a different day 
  dayInt=parseInt(dayString,10);
  dayInt+=additionalDayOffset;
  if(dayInt<0)
  {
    // can't set day yet, we need to set it to the last day of the month but we need to know which month first
    additionalMonthOffset--;
  }
  
  
  
  // we need to adjust if we've passed the end of the month
  
  
  
  // get month and adjust for 0 versus 1 based logic. Also adjust for any offset caused by
  // days rolling back or forward into a different month
  monthInt=parseInt(monthString,10);
  // Month is 1-based in the string but 0-based in JavaScript
  monthInt--;
  monthInt+=additionalMonthOffset;
  if(monthInt<0)
  {
    monthInt=11;
    additionalYearOffset--;
  }

  // get years and adjust for any offset caused by month rolling back or forward into a
  // different year
  yearInt=parseInt(yearString,10);
  yearInt+=additionalYearOffset;
  
  // the month changed backward, figure out the last day of the month for the month was are in
  if(additionalMonthOffset<0)
  {
    dayInt=monthdays(monthInt);

    // account for leap years
    isLeap = new Date(yearInt,1,29).getDate() == 29;
    if(isLeap && monthInt==1)
      dayInt++;
  }
  
  // the month rolled forward, set the day to 1
  if(additionalMonthOffset>0)
    dayInt=1;
  
  
  RetVal.setUTCFullYear(yearInt);
  RetVal.setUTCMonth(monthInt);
  RetVal.setUTCDate(dayInt);
  RetVal.setUTCHours(hourInt);
  RetVal.setUTCMinutes(minuteInt);
  RetVal.setUTCSeconds(secondInt);
  RetVal.setUTCMilliseconds(0);
  
  //alert("From HELI: "+RetVal.toLocaleDateString()+"__"+RetVal.toLocaleTimeString());
  
  // returns date as a LOCAL, it was UTC
  
  return(RetVal);
}

function createDateFromThunderNoteDateString(theDate)
{
  // 6-28-2008 12:41:42

  var RetVal=new Date();

  var twoParts=theDate.split(' ');
  var threeParts;
  var yearString;
  var monthString;
  var dayString;
  var hourString;
  var minuteString;
  var secondString;
  var tmpVal;
  
  var yearInt,monthInt,dayInt,hourInt,minuteInt,secondInt;
  
  threeParts=twoParts[0].split('-');
  monthString=threeParts[0];
  dayString=threeParts[1];
  yearString=threeParts[2];
  
  threeParts=twoParts[1].split(':');
  hourString=threeParts[0];
  minuteString=threeParts[1];
  secondString=threeParts[2];
  
  // get seconds
  secondInt=parseInt(secondString,10);

  // get minutes
  minuteInt=parseInt(minuteString,10);

  // get hours 
  hourInt=parseInt(hourString,10);
  
  // get days
  dayInt=parseInt(dayString,10);
  
  // get month
  monthInt=parseInt(monthString,10)-1;

  // get years
  yearInt=parseInt(yearString,10);  
  
  RetVal.setFullYear(yearInt);
  RetVal.setMonth(monthInt);
  RetVal.setDate(dayInt);
  RetVal.setHours(hourInt);
  RetVal.setMinutes(minuteInt);
  RetVal.setSeconds(secondInt);
  RetVal.setMilliseconds(0);

  // return date as local, it was local already
  
  return(RetVal);
}

function createThunderNoteDateFromDateObject(dateObject)
{
  // 6-28-2008 12:41:42
  var year=dateObject.getFullYear();
  var month=dateObject.getMonth()+1;
  var day=dateObject.getDate();
  var hour=dateObject.getHours();
  var minute=dateObject.getMinutes();
  var second=dateObject.getSeconds();

  var RetVal=month+"-"+returnPaddedNumber(day,2)+"-"+year+" "+hour+":"+returnPaddedNumber(minute,2)+":"+returnPaddedNumber(second,2);
  
  alert("To TN: "+RetVal);
  
  return(RetVal);
}

function createHelipadDateFromDateObject(dateObject,offsetHours,offsetMinutes)
{
  // 2009-08-31T01:05:11+01:00
  // 30th 20:05 EST
  var d2=new Date();
  var l2=dateObject.getTime();
  // local offset from UTC
  var localOffset = d2.getTimezoneOffset() * 60000;
  
  // Helipad offset from UTC
  var ofh,ofm;
  
  if(offsetHours==-1)     ofh=__HELIPAD._helipadDefaultOffsetHours; else ofh=offsetHours;
  if(offsetMinutes==-1)   ofm=__HELIPAD._helipadDefaultOffsetMinutes; else ofm=offsetMinutes;
  
  var totalOffsetMinutes=(ofh*60)+ofm;
  
  l2+=localOffset;
  l2+=totalOffsetMinutes*60000;
  
  d2.setTime(l2);
  var year=d2.getFullYear();
  var month=d2.getMonth()+1;
  var day=d2.getDate();
  var hour=d2.getHours();
  var minute=d2.getMinutes();
  var second=d2.getSeconds();

  var RetVal=year+"-"+returnPaddedNumber(month,2)+"-"+returnPaddedNumber(day,2)+"T"+returnPaddedNumber(hour,2)+":"+returnPaddedNumber(minute,2)+":"+returnPaddedNumber(second,2)+"+"+returnPaddedNumber(ofh,2)+":"+returnPaddedNumber(ofm,2)+"";
  
  return(RetVal);
}

function returnPaddedNumber(num,places)
{
  var RetVal=""+num;
  
  while(RetVal.length<places)
    RetVal="0"+RetVal;
    
  return(RetVal);
}